<?php

namespace Shop\AdminBundle\Controller;

use Symfony\Component\HttpFoundation\Request;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;

use Shop\AdminBundle\Controller\BaseController;

use Shop\AdminBundle\Entity\AdminRole;
use Shop\AdminBundle\Form\AdminRoleType;

/**
 * AdminRole controller.
 *
 * @Route("/admin/privilege")
 */
class AdminRoleController extends BaseController
{
    /**
     * 显示权限列表，页面
     *
     * @Route("/", name="admin_role")
     * @Method("GET")
     * @Template("ShopAdminBundle:AdminRole:index.html.twig")
     */
    public function indexAction()
    {
        $repo=$this->getRepo('adminRole');
        return $this->paginate($repo);
    }

    /**
     * 添加权限，表单处理
     *
     * @Route("/", name="admin_role_create")
     * @Method("POST")
     */
    public function createAction(Request $request)
    {
        $entity = new AdminRole();
        $form = $this->createForm(new AdminRoleType(), $entity, array(
            'action' => $this->generateUrl('admin_role_create'),
            'method' => 'POST',
        ));
        $form->handleRequest($request);

        if ($form->isValid()) {
            $em = $this->getDoctrine()->getManager();
            $entity->setPrivileges(implode(',',$entity->getPrivileges()));

            $em->persist($entity);
            $em->flush();

            return $this->redirect($this->generateUrl('admin_role'));
        }

        return array(
            'entity' => $entity,
            'form'   => $form->createView(),
        );
    }

    /**
     * 添加权限，页面
     *
     * @Route("/new", name="admin_role_new")
     * @Method("GET")
     * @Template()
     */
    public function newAction()
    {
        $entity = new AdminRole();
        $form = $this->createForm(new AdminRoleType(), $entity, array(
            'action' => $this->generateUrl('admin_role_create'),
            'method' => 'POST',
        ));

        return array(
            'entity' => $entity,
            'form'   => $form->createView(),
        );
    }

    /**
     * 编辑权限，页面
     *
     * @Route("/{id}/edit", name="admin_role_edit")
     * @Method("GET")
     * @Template("ShopAdminBundle:AdminRole:new.html.twig")
     */
    public function editAction($id)
    {
        $em = $this->getDoctrine()->getManager();

        $entity = $em->getRepository('ShopAdminBundle:AdminRole')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find AdminRole entity.');
        }

        $editForm = $this->createForm(new AdminRoleType(), $entity, array(
            'action' => $this->generateUrl('admin_role_update', array('id' => $entity->getId())),
            'method' => 'POST',
        ));

        return array(
            'entity'      => $entity,
            'edit_form'   => $editForm->createView()
        );
    }

    /**
     * 更新权限，表单处理
     *
     * @Route("/{id}", name="admin_role_update")
     * @Method("POST")
     * @Template()
     */
    public function updateAction(Request $request, $id)
    {
        $em = $this->getDoctrine()->getManager();
        $entity = $em->getRepository('ShopAdminBundle:AdminRole')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find AdminRole entity.');
        }

        $editForm = $this->createForm(new AdminRoleType(), $entity, array(
            'action' => $this->generateUrl('admin_role_update', array('id' => $entity->getId())),
            'method' => 'POST',
        ));

        $editForm->handleRequest($request);

        if ($editForm->isValid()) {
            $entity->setPrivileges(implode(',',$entity->getPrivileges()));
            $em->persist($entity);
            $em->flush();
            return $this->redirect($this->generateUrl('admin_role', array('id' => $id)));
        }

        return array(
            'entity'      => $entity,
            'edit_form'   => $editForm->createView()
        );
    }

    /**
     * Deletes a AdminRole entity.
     *
     * @Route("/{id}/delete", name="admin_role_delete")
     * @Method("GET")
     */
    public function deleteAction(Request $request, $id)
    {
        return $this->deleteRow('adminRole',$request, $id);
    }
}
